using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BTEditor
{
    [BTComposite]
    public class Sequence : Composite
    {
        public override Type GetNodeType()
        {
            return typeof(NPBehave.Sequence);
        }

        public override ErrorType GetIsVaild()
        {
            if (BelongNode == null)
            {
                return ErrorType.Error;
            }
            return BelongNode.IsHaveChild ? ErrorType.None : ErrorType.Error;
        }

        public override string ToChild()
        {
            StringBuilder sb = new StringBuilder();
            BelongNode.ChildNodeList.Sort(BTNodeSort);
            List<EditorNode> editorNodes = BelongNode.ChildNodeList.Select(x => x.Node).ToList();
            sb.Append($"new {BTUtils.GetTypeName(GetNodeType())}");
            sb.Append("( \n");
            for (int i = 0; i < editorNodes.Count; i++)
            {
                if (editorNodes.Count - 1 == i)
                {
                    sb.Append($"{editorNodes[i].ToChild()}");
                }
                else
                {
                    sb.Append($"{editorNodes[i].ToChild()},");
                }
            }

            sb.Append("\n \t)");
            return sb.ToString();
        }
    }
}